{% extends 'base.html' %}

{% block body %}
<!-- UNVALIDATED USERS BLOCK -->
{% if unvalidatedUsersArr %}
  <div class='box'>
    {{ 'There are users waiting for validation of their account:'|trans|msg('warning', false) }}
    <ul>
    {% for user in unvalidatedUsersArr %}
      <li>
        {{ user.fullname|raw }} ({{ user.email }}) <button class='button btn btn-primary usersValidate' data-userid='{{ user.userid }}'>{{ 'Validate'|trans }}</button>
      </li>
    {% endfor %}
    </ul>
  </div>
{% endif %}

<!-- ADMIN PANEL MAIN MENU -->
<ul id='menu'>
  <li class='tabhandle' id='tab1'>{{ 'Team'|trans }}</li>
  <li class='tabhandle' id='tab2'>{{ 'Groups'|trans }}</li>
  <li class='tabhandle' id='tab3'>{{ 'Users'|trans }}</li>
  <li class='tabhandle' id='tab4'>{% trans %}Status{% plural 2 %}Status{% endtrans %}</li>
  <li class='tabhandle' id='tab5'>{{ 'Types of items'|trans }}</li>
  <li class='tabhandle' id='tab6'>{{ 'Experiments template'|trans }}</li>
  <li class='tabhandle' id='tab7'>{{ 'Import'|trans }}</li>
  <li class='tabhandle' id='tab8'>{{ 'Tag manager'|trans }}</li>
</ul>

<!-- TAB 1 TEAM CONFIG -->
<div class='divhandle' id='tab1div'>
  <div class='box'>
    <h3>{{ 'Configure your Team'|trans }}</h3>
    <hr>
    <form class='form-group' method='post' action='app/controllers/AdminController.php' autocomplete='off'>
      <input type='hidden' value='true' name='teamsUpdateFull' />
      {{ App.Csrf.getHiddenInput|raw }}
      <label for='deletable_xp'>{{ 'Users can delete experiments:'|trans }}</label>
      <select class='form-control col-md-4' name='deletable_xp' id='deletable_xp'>
        <option value='1'{{ App.teamConfigArr.deletable_xp ? " selected='selected'" }}>{{ 'Yes'|trans }}</option>
        <option value='0'{{ not App.teamConfigArr.deletable_xp ? " selected='selected'" }}>{{ 'No'|trans }}</option>
      </select>
      <span class='smallgray'>{{ 'An admin account will always be able to delete experiments.'|trans }}</span>

      <label for='public_db'>{{ 'Anonymous visitors can see database items:'|trans }}</label>
      <select class='form-control col-md-4' name='public_db' id='public_db'>
        <option value='1'{{ App.teamConfigArr.public_db ? " selected='selected'" }}>{{ 'Yes'|trans }}</option>
        <option value='0'{{ not App.teamConfigArr.public_db ? " selected='selected'" }}>{{ 'No'|trans }}</option>
      </select>

      <label for='link_name'>{{ 'Name of the link in the top menu:'|trans }}</label>
      <input class='form-control col-md-4' type='text' value='{{ App.teamConfigArr.link_name }}' name='link_name' id='link_name' />

      <label for='link_href'>{{ 'Address where this link should point:'|trans }}</label>
      <input class='mb-4 form-control col-md-4' type='text' value='{{ App.teamConfigArr.link_href }}' name='link_href' id='link_href' />

      <label for='do_force_canread'>{{ 'Enforce read permissions?'|trans }}</label>
      <select class='form-control col-md-4' name='do_force_canread' id='do_force_canread'>
        <option value='1'{{ App.teamConfigArr.do_force_canread ? " selected='selected'" }}>{{ 'Yes'|trans }}</option>
        <option value='0'{{ not App.teamConfigArr.do_force_canread ? " selected='selected'" }}>{{ 'No'|trans }}</option>
      </select>
      <span class='smallgray'>{{ 'Select if you want to enforce read permissions on experiments'|trans }}</span>

      <label for='force_canread'>{{ 'Enforced read permissions on experiments'|trans }}</label>
      <select class='form-control col-md-4' name='force_canread' id='force_canread'>
        <option value='public'{{ App.teamConfigArr.force_canread == 'public' ? " selected='selected'" }}>{{ 'Public'|trans }}</option>
        <option value='organization'{{ App.teamConfigArr.force_canread == 'organization' ? " selected='selected'" }}>{{ 'Organization'|trans }}</option>
        <option value='team'{{ App.teamConfigArr.force_canread == 'team' ? " selected='selected'" }}>{{ 'Team'|trans }}</option>
        <option value='user'{{ App.teamConfigArr.force_canread == 'user' ? " selected='selected'" }}>{{ 'User'|trans }}</option>
      </select>
      <span class='smallgray'>{{ 'Enforce the read permission for all experiments of users in this team.'|trans }}</span>

      <label for='do_force_canwrite'>{{ 'Enforce write permissions?'|trans }}</label>
      <select class='form-control col-md-4' name='do_force_canwrite' id='do_force_canwrite'>
        <option value='1'{{ App.teamConfigArr.do_force_canwrite ? " selected='selected'" }}>{{ 'Yes'|trans }}</option>
        <option value='0'{{ not App.teamConfigArr.do_force_canwrite ? " selected='selected'" }}>{{ 'No'|trans }}</option>
      </select>
      <span class='smallgray'>{{ 'Select if you want to enforce write permissions on experiments'|trans }}</span>

      <label for='force_canwrite'>{{ 'Enforced write permissions on experiments'|trans }}</label>
      <select class='form-control col-md-4' name='force_canwrite' id='force_canwrite'>
        <option value='public'{{ App.teamConfigArr.force_canwrite == 'public' ? " selected='selected'" }}>{{ 'Public'|trans }}</option>
        <option value='organization'{{ App.teamConfigArr.force_canwrite == 'organization' ? " selected='selected'" }}>{{ 'Organization'|trans }}</option>
        <option value='team'{{ App.teamConfigArr.force_canwrite == 'team' ? " selected='selected'" }}>{{ 'Team'|trans }}</option>
        <option value='user'{{ App.teamConfigArr.force_canwrite == 'user' ? " selected='selected'" }}>{{ 'User'|trans }}</option>
      </select>
      <span class='smallgray'>{{ 'Enforce the write permission for all experiments of users in this team.'|trans }}</span>

      <span class='m-0 button btn btn-neutral togglableNext'>{{ 'Override general timestamping config'|trans }}</span>
      <div class='togglableHidden mt-2'>
        <label for='stampprovider'>{{ 'URL for external timestamping service:'|trans }}</label>
        <input class='form-control col-md-4' type='url' placeholder='http://zeitstempel.dfn.de/' value='{{ App.teamConfigArr.stampprovider }}' name='stampprovider' id='stampprovider' />
        <span class='smallgray'>{{ 'This should be the URL used for %sRFC 3161%s-compliant timestamping requests.'|trans|format("<a href='https://tools.ietf.org/html/rfc3161'>", "</a>")|raw }}</span>

        <!-- TODO provide an upload button -->
        <label for='stampcert'>{{ 'Chain of certificates of the external timestamping service:'|trans }}</label>
        <input class='form-control col-md-4' type='text' placeholder='app/dfn-cert/pki.dfn.pem' value='{{ App.teamConfigArr.stampcert }}' name='stampcert' id='stampcert' />
        <span class='smallgray'>{{ "This should point to the chain of certificates used by your external timestamping provider to sign the timestamps.%sLocal path relative to eLabFTW installation directory. You should add it in the 'uploads' folder. The file needs to be in %sPEM-encoded (ASCII)%s format!"|trans|format("<br>", "<a href='https://en.wikipedia.org/wiki/Privacy-enhanced_Electronic_Mail'>", "</a>")|raw }}</span>
        <label for='stamplogin'>{{ 'Login for external timestamping service:'|trans }}</label>
        <input class='form-control col-md-4' autocomplete='off' type='text' value='{{ App.teamConfigArr.stamplogin }}' name='stamplogin' id='stamplogin' />
        <span class='smallgray'>{{ 'This should be the login associated with your timestamping service provider'|trans }}</span>

        <label for='stamppass'>{{ 'Password for external timestamping service:'|trans }}</label>
        {% if App.teamConfigArr.stamppass|length > 1 %}
        <p>{{ 'A password is already set.'|trans }}
        {{ '%sClear it%s or change it below:'|trans|format("
        <a href='app/controllers/AdminController.php?clearStamppass=1'>",
        "</a>"
        )|raw }}
        </p>
        {% endif %}
        <input class='form-control col-md-4' autocomplete='off' type='password' name='stamppass' id='stamppass' />
        <span class='smallgray'>{{ 'Your timestamping service provider password'|trans }}</span>
      </div>

      <div class='submitButtonDiv'>
        <button type='submit' class='button btn btn-primary'>Save</button>
      </div>
    </form>
  </div>
</div>

<!-- TAB 2 TEAM GROUPS -->
<div class='divhandle' id='tab2div'>
  <div class='box form-group'>
    <h3>{{ 'Manage Groups of Users'|trans }}</h3>
    <hr>

    <!-- CREATE A GROUP -->
    <label for='teamGroupCreate'>{{ 'Create a group'|trans }}</label>
    <input class='form-control col-md-4 custom-control-inline' id='teamGroupCreate' type='text' />
    <button type='submit' id='teamGroupCreateBtn' class='button btn btn-primary'>{{ 'Create'|trans }}</button>
    <!-- END CREATE GROUP -->

    <!-- SHOW EXISTING GROUPS -->
    <div id='team_groups_div'>
    <hr>
      <h3>{{ 'Existing groups'|trans }}</h3>
      {% for teamGroup in teamGroupsArr %}
        <div class='box'>
          <a href='#'>
            <i class='fas fa-trash-alt clickable teamGroupDelete align_right' data-id='{{ teamGroup.id }}'></i>
          </a>
          <h3 title='Click to Edit' class='inline editable teamgroup_name' data-id='{{ teamGroup.id }}'>{{ teamGroup.name }}</h3>
          {% for user in teamGroup.users %}
            <span class='user-badge clickable rmUserFromGroup' data-user='{{ user.userid }}' data-group='{{ teamGroup.id }}'>{{ user.fullname|raw }}</span>
          {% endfor %}
          <input class='form-control col-md-4 addUserToGroup d-block mt-2' autocomplete='off' data-group={{ teamGroup.id }} placeholder='{{ 'Add user'|trans }}' type='text' />
        </div>
      {% endfor %}
    </div>
  </div>
</div>
<!-- END TEAM GROUPS -->

<!-- TAB 3 USERS -->
<div class='divhandle' id='tab3div'>
  {{ include('editusers.html') }}
</div>

<!-- TAB 4 STATUS -->
<div class='divhandle' id='tab4div'>
  <div class='box'>
    <h3>{{ 'Add a New Status'|trans }}</h3>
    <hr>
    <div class='form-row align-items-center'>
      <div class='col-auto'>
        <label for='statusName'>{{ 'Name'|trans }}</label>
        <input class='form-control' type='text' id='statusName' />
      </div>

      <div class='col-1'>
        <label for='statusColor'>{{ 'Color'|trans }}</label>
        <input class='form-control randomColor' type='color' id='statusColor' />
      </div>

      <div class='col-auto'>
        <div class='form-check mt-4'>
          <input class='form-check-input' type='checkbox' id='statusTimestamp' />
          <label class='form-check-label' for='statusTimestamp'>{{ 'Allow timestamp'|trans }}</label>
        </div>
      </div>

      <div class='col-auto mt-4'>
        <button class='button btn btn-primary' id='statusCreate'>{{ 'Save'|trans }}</button>
      </div>
    </div>
  </div>

  <div class='box'>
    <h3>{{ 'Edit an Existing Status'|trans }}</h3>
    <hr>
    <ul class='list-group sortable' data-axis='y' data-table='status'>
      {% for status in statusArr %}
        <li id='status_{{ status.category_id }}' class='list-group-item'>
          <ul class='list-inline'>
            <li class='list-inline-item'><label for='statusName_{{ status.category_id }}'>{{ 'Name'|trans }}</label> <input class='form-control' required type='text' id='statusName_{{ status.category_id }}' value='{{ status.category }}' /></li>
            <li class='list-inline-item col-1 align-top'><label for='statusColor_{{ status.category_id }}'>{{ 'Color'|trans }}</label> <input class='form-control' type='color' id='statusColor_{{ status.category_id }}' value='#{{ status.color }}' />
            </li>
            <li class='list-inline-item'><input type='checkbox' id='statusTimestamp_{{ status.category_id }}' {{ status.is_timestampable ? " checked" }} /> <label for='statusTimestamp_{{ status.category_id }}'>{{ 'Allow timestamp'|trans }}</label></li>
            <li class='list-inline-item'><input type='radio' name='defaultRadio' id='statusDefault_{{ status.category_id }}' {{ status.is_default ? " checked" }} /> <label for='statusDefault_{{ status.category_id }}'>{{ 'Default status'|trans }}</label></li>
            <li class='list-inline-item'><button data-id='{{ status.category_id }}' class='button btn btn-primary statusSave'>{{ 'Save'|trans }}</button></li>

            <li class='list-inline-item'><button data-id='{{ status.category_id }}' class='button btn btn-danger statusDestroy' >{{ 'Delete'|trans }}</button></li>
            <li class='list-inline-item'><i class='fas fa-sort draggable sortableHandle align_right fa-2x'></i></li>
          </ul>
        </li>
      {% endfor %}
    </ul>
  </div>
</div>

<!-- TAB 5 ITEMS TYPES-->
<div class='divhandle' id='tab5div'>
  <div class='box'>
    <h3>{{ 'Add a new type of item'|trans }}</h3>
    <hr>
    <div class='form-row align-items-center mb-2'>
      <div class='col-auto'>
        <label for='itemsTypesName'>{{ 'Name'|trans }}</label>
        <input class='form-control' type='text' id='itemsTypesName' />
      </div>

      <div class='col-1'>
        <label for='itemsTypesColor'>{{ 'Color'|trans }}</label>
        <input class='form-control randomColor' type='color' id='itemsTypesColor' />
      </div>

      <div class='col-auto'>
        <div class='form-check mt-4'>
          <input class='form-check-input' type='checkbox' id='itemsTypesBookable' />
          <label class='form-check-label' for='itemsTypesBookable'>{{ 'Bookable'|trans }} {{ 'in the %sscheduler%s'|trans|format("<a href='team.php'>", "</a>")|raw }}</label>
        </div>
      </div>
    </div>

    <textarea class='mceditable' id='itemsTypesTemplate'></textarea>
    <div class='submitButtonDiv'>
      <button id='itemsTypesCreate' class='button btn btn-primary'>{{ 'Save'|trans }}</button>
    </div>
  </div>


  <div class='box'>
    <h3>{{ 'Database Items Types'|trans }}</h3>
    <hr>
    <ul class='list-group form-group draggable sortable' data-axis='y' data-table='items_types'>
      {% for itemType in itemsTypesArr %}
        <li id='itemstypes_{{ itemType.category_id }}' class='list-group-item'>
          <ul class='list-inline'>
            <li class='list-inline-item'>
              <label for='itemsTypesName_{{ itemType.category_id }}'>{{ 'Name'|trans }}</label>
              <input type='text' class='form-control' id='itemsTypesName_{{ itemType.category_id }}' value='{{ itemType.category }}' />
            </li>

            <li class='list-inline-item col-1 align-top'>
              <label for='itemsTypesColor_{{ itemType.category_id }}'>{{ 'Color'|trans }}</label>
              <input class='form-control' type='color' id='itemsTypesColor_{{ itemType.category_id }}' value='#{{ itemType.color }}' />
            </li>
            <li class='list-inline-item'>
              <input id='itemsTypesBookable_{{ itemType.category_id }}' type='checkbox' {{ itemType.bookable ? " checked" }} />
              <label for='itemsTypesBookable_{{ itemType.category_id }}'>{{ 'Bookable'|trans }}</label>
            </li>
            <li class='list-inline-item'>
              <button data-id='{{ itemType.category_id }}' class='button btn btn-neutral itemsTypesShowEditor'>{{ 'Edit the template'|trans }}</button>
            </li>
            <li class='list-inline-item'>
              <button data-id='{{ itemType.category_id }}' class='button btn btn-primary itemsTypesUpdate'>{{ 'Save'|trans }}</button>
            </li>
            <li class='list-inline-item'>
              <button data-id='{{ itemType.category_id }}' class='button btn btn-danger itemsTypesDestroy'>{{ 'Delete'|trans }}</button>
            </li>
            <li class='list-inline-item mt-2 itemsTypesEditor' id='itemsTypesEditor_{{ itemType.category_id }}'>
              <textarea class='' id='itemsTypesTemplate_{{ itemType.category_id }}'>{{ itemType.template }}</textarea>
            </li>
            <li class='list-inline-item'><i class='fas fa-sort sortableHandle align_right fa-2x'></i></li>
          </ul>
        </li>
      {% endfor %}
    </ul>
  </div>
</div>

<!-- TAB 6 COMMON EXPERIMENT TEMPLATE -->
<div class='divhandle' id='tab6div'>
  <div class='box'>
    <h3>{{ 'Common Experiment Template'|trans }}</h3><hr>
    <p>{{ 'This is the default text when someone creates an experiment.'|trans }}</p>
    <textarea style='height:400px' class='mceditable' id='commonTplTemplate'>
      {{ commonTplBody|raw }}
    </textarea>
    <div class='submitButtonDiv'>
      <button type='submit' class='button btn btn-primary'>{{ 'Save'|trans }}</button>
    </div>
  </div>
</div>

<!-- TAB 7 IMPORT -->
<div class='divhandle' id='tab7div'>
  <div class='box'>
    <h3>{{ 'Import a CSV File'|trans }}</h3>
    <hr>
    <p style='text-align:justify'>
      {{ "Import a .csv (Excel spreadsheet) file into the database. First you need to open your .xls/.xlsx file in Excel or Libreoffice and save it as .csv. IMPORTANT: one of the column MUST be named 'title'. You should make a tiny import of 3 lines to see if everything works before importing a big file."|trans }}
    </p>

    <form class='form-group' enctype='multipart/form-data' action='app/controllers/ImportController.php' method='post'>
      <label for='target_csv'>{{ '1. Select a type of item to import to:'|trans }}</label>
      <select class='form-control col-md-4' id='target_csv' autocomplete='off' name='target'>
        <option selected disabled>--------</option>
        {% for itemsType in itemsTypesArr %}
        <option value='{{ itemsType.category_id }}'>{{ itemsType.category }}</option>
        {% endfor %}
      </select>

      <label for='visibility_csv'>{{ '2. Select a visibility:'|trans }}</label>
      <select class='form-control col-md-4' id='visibility_csv' autocomplete='off' name='visibility'>
        {% for key, value in visibilityArr %}
          <option value='{{ key }}'
          {{ 'team' == key ? ' selected' }}
          >{{ value|trans }}</option>
        {% endfor %}
      </select>

      <label for='separator_csv'>{{ '3. Select a delimiter:'|trans }}</label>
      <select class='form-control col-md-4' id='separator_csv' autocomplete='off' name='delimiter'>
        <option value=';'>; ({{ 'semicolon'|trans }})</option>
        <option value=','>, ({{ 'comma'|trans }})</option>
        <option value='tab'>{{ 'Tab'|trans }}</option>
      </select>

      {{ App.Csrf.getHiddenInput|raw }}
      <label for='csvuploader' class='d-block'>{{ '4. Select a CSV file to import:'|trans }}</label>
      <input id='csvuploader' name='file' type='file' accept='.csv' />
      <input name='type' type='hidden' value='csv' />
      <div class='submitButtonDiv'>
        <button type='submit' class='button btn btn-primary'>{{ 'Import CSV'|trans }}</button>
      </div>
    </form>
  </div>

  <div class='box'>
    <h3>{{ 'Import a ZIP File'|trans }}</h3>
    <hr>
    <p>
      {{ "Import a .elabftw.zip archive."|trans }} <span class='font-weight-bold'>{{ 'You should make a backup of your database before importing thousands of items!'|trans }}</span>
    </p>

    <form class='form-group' enctype='multipart/form-data' action='app/controllers/ImportController.php' method='post'>
      <label for='target_zip'>{{ '1. Select where to import:'|trans }}</label>
      <select class='form-control col-md-4' id='target_zip' autocomplete='off' name='target'>
        <option selected disabled>-------</option>
        <option disabled>{{ 'Import in database'|trans }}</option>
        {% for itemsType in itemsTypesArr %}
          <option value='{{ itemsType.category_id }}'>{{ itemsType.category }}</option>
        {% endfor %}
        <option disabled>{{ 'Import in experiments'|trans }}</option>
        {% for user in allTeamUsersArr %}
          <option value='{{ user.userid }}'>{{ user.fullname|raw }}</option>
        {% endfor %}
      </select>

      <label for='visibility_zip'>{{ '2. Select a visibility:'|trans }}</label>
      <select class='form-control col-md-4' id='visibility_zip' autocomplete='off' name='visibility'>
        {% for key, value in visibilityArr %}
          <option value='{{ key }}'
          {{ 'team' == key ? ' selected' }}
          >{{ value|trans }}</option>
          {% endfor %}
      </select>

      {{ App.Csrf.getHiddenInput|raw }}
      <label class='d-block' for='zipuploader'>{{ '3. Select a ZIP file to import:'|trans }}</label>
      <input id='zipuploader' name='file' type='file' accept='.zip' />
      <input name='type' type='hidden' value='zip' />

      <div class='submitButtonDiv'>
        <button type='submit' class='button btn btn-primary'>{{ 'Import ZIP'|trans }}</button>
      </div>

    </form>
  </div>
</div>

<!-- TAB 8 TAG MANAGER -->
<div class='divhandle' id='tab8div'>
  <div class='box'>
    <h3>{{ 'Manage tags of the team'|trans }}</h3>
    <p>{{ 'From here you can edit or delete the tags for your team. Click the tag to edit it. Then click the deduplicate button to merge similar tags.'|trans }}</p>
    <button class='button btn btn-primary tagDeduplicate'><i class='fas fa-clone mr-1 text-white'></i>{{ 'Deduplicate'|trans }}</button>
    <hr>
    <div class='mt-2' id='tag_manager'>
      {% for tag in tagsArr %}
        <p><i class='fas fa-trash-alt clickable tagDestroy mr-1' title='Delete' data-tagid='{{ tag.id }}'></i> <span class='tag-editable editable' data-tagid='{{ tag.id }}'>{{ tag.tag|raw }}</span></p>
      {% endfor %}
    </div>
  </div>
</div>

{% endblock body %}
